package com.woniu.service.impl;

import com.woniu.dao.FinalTrialDao;
import com.woniu.entity.*;
import com.woniu.service.FinalTrialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;

@Service
public class FinalTrialServiceImpl implements FinalTrialService {
    @Autowired
    FinalTrialDao finalTrialDao;

    @Override
    public List<FinalTrialClientInfoVO> listAllApplicant() {
        List<FinalTrialClientInfoVO> allInfo = finalTrialDao.firstClientProduction();
        return allInfo;
    }

    @Override
    public List<FinalTrialDetailVO> getDetailInfo(Integer clientId) {

        return finalTrialDao.getDetailInfo(clientId);
    }

    @Override
    public List<HistoryEntity> getHistoryList(Integer clientId) {
        List<HistoryEntity> result = new ArrayList<>();
        List<HistoryEntity> historyList = finalTrialDao.getHistoryList(clientId);
        if (Objects.nonNull(historyList)){
            List<Integer> ids = new ArrayList<>();
            historyList.forEach((e)->{
                ids.add(e.getLoanInfoId());
            });

            List<BillPaymentStatusEntity> loanStatus = finalTrialDao.findAllLoanStatus(clientId,ids);
            System.out.println("historyList"+historyList);
            System.out.println("loanStatus"+loanStatus);

            loanStatus.forEach((e)->{
                historyList.forEach((item)->{
                    if(e.getLoanInfoId().equals(item.getLoanInfoId())){
                        if(e.getPaymentStatus()>0){
                            item.setBillPaymentStatus(1);
                        }else {
                            item.setBillPaymentStatus(0);
                        }
                    }
                });
            });
            historyList.forEach((e)->{
                if(Objects.nonNull(e.getBillPaymentStatus())){
                    result.add(e);
                }
            });
        }

        return result;
    }

    @Override
    public Integer updateFinalTrialOrder(Map<String,Object> params) {
        return finalTrialDao.updateFinalTrialOrder(params);
    }

    @Override
    public List<FinalTrialClientInfoVO> searchClientProduction(Map<String, Object> params) {
        return finalTrialDao.searchClientProduction(params);
    }

    @Override
    public List<FinalTrialExportVO> selectFinalTrialOrders(UsualExportVO data) {
        return finalTrialDao.selectFinalTrialOrders(data);
    }
}
